package com.wmh.baseservice.merchant.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.common.annotation.parameterverify.ParameterVerify;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.pojo.PageSortRoles;
import com.wmh.baseservice.common.utils.page.pojo.SortWay;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ResultCodeInfo;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.merchant.pojo.MerchantFreightTemplate;
import com.wmh.baseservice.merchant.pojo.MerchantGroupGoods;
import com.wmh.baseservice.merchant.service.MerchantFreightTemplateService;
import com.wmh.baseservice.merchant.service.MerchantGroupGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wmh generator
 * @since 2022-04-15
 */
@Api(tags = "商家端-运费模板相关接口")
@RestController
@RequestMapping(value = "merchant/merchantFreightTemplate", produces = "text/plain;charset=utf-8")
public class MerchantFreightTemplateController {

    @Resource
    private MerchantFreightTemplateService merchantFreightTemplateService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private MerchantGroupGoodsService merchantGroupGoodsService;

    @ApiOperation("模板列表")
    @PostMapping("list")
    public String list(PageInfo pageInfo, Integer sort, String remark, String createTimeScope) {
        Long id = (Long) request.getAttribute("id");
        if (CommUtil.checkNull(pageInfo.getPageSize())){
            // 设置分页排序规则
            PageUtil.setPageSortRoles(pageInfo,sort,"create_time");
            //执行分页辅助工具
            PageResult<MerchantFreightTemplate> pageResult = new PageUtil<MerchantFreightTemplate>(pageInfo).startPage((page, queryWrapper) -> {
                queryWrapper.eq(MerchantFreightTemplate.MERCHANT_ID,id);
                if(CommUtil.checkNull(remark)){
                    queryWrapper.like(MerchantFreightTemplate.REMARK,remark);
                }
                if(CommUtil.checkNull(createTimeScope)){
                    List<String> list = JSON.parseArray(createTimeScope, String.class);
                    queryWrapper.between(MerchantFreightTemplate.CREATE_TIME,list.get(0),list.get(1));
                }
                merchantFreightTemplateService.page(page, queryWrapper);
            });
            return ReturnBody.success(pageResult);
        }else{
            QueryWrapper<MerchantFreightTemplate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(MerchantFreightTemplate.MERCHANT_ID,id);
            if(CommUtil.checkNull(remark)){
                queryWrapper.like(MerchantFreightTemplate.REMARK,remark);
            }
            if(CommUtil.checkNull(createTimeScope)){
                List<String> list = JSON.parseArray(createTimeScope, String.class);
                queryWrapper.between(MerchantFreightTemplate.CREATE_TIME,list.get(0),list.get(1));
            }
            List<MerchantFreightTemplate> list = merchantFreightTemplateService.list(queryWrapper);
            return ReturnBody.success(list);
        }
    }

    @ApiOperation("添加或修改模板")
    @PostMapping("add")
    @ParameterVerify(notNull = {"merchantFreightTemplate.locationIds", "merchantFreightTemplate.areaPrice", "merchantFreightTemplate.defaultPrice"})
    public String add(MerchantFreightTemplate merchantFreightTemplate) {
        Long id = (Long) request.getAttribute("id");
        merchantFreightTemplate.setMerchantId(id);
        if (!merchantFreightTemplateService.saveOrUpdate(merchantFreightTemplate)) {
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        return ReturnBody.success();
    }

    @ApiOperation("删除模板")
    @PostMapping("remove")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "模板id", required = true),
    })
    @ParameterVerify(notNull = "id")
    public String remove(Long id) {
        Long merchantId = (Long) request.getAttribute("id");
        MerchantFreightTemplate merchantFreightTemplate = merchantFreightTemplateService.getByIdForUpdate(id);
        if (!CommUtil.checkNull(merchantFreightTemplate) || !merchantFreightTemplate.getMerchantId().equals(merchantId)) {
            return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
        }
        //判断该模板是否正在被使用中
        int count = merchantGroupGoodsService.count(new QueryWrapper<MerchantGroupGoods>().eq(MerchantGroupGoods.FREIGHT_ID, id));
        if (count != 0) {
            return ReturnBody.error("该模板正在被使用中，无法删除");
        }
        if (!merchantFreightTemplateService.removeById(id)) {
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        return ReturnBody.success();
    }

}
